package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.base.utils.SPHelper;
import com.google.gson.Gson;
import com.support.toast.ToastUtil;

import casa.depesos.app.com.App;
import casa.depesos.app.com.activity.LoginActivity;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.RegBean;
import casa.depesos.app.com.http.bean.WhiteDataBean;
import casa.depesos.app.com.http.req.GetWhiteReq;
import casa.depesos.app.com.http.req.RegReq;
import casa.depesos.app.com.http.req.no.SloganReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;

public class SetPwdFragment extends NewPwdFragment {

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Report.show("ACTION_GG_011");
        new SloganReq().req();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        binding.pwd.setHint("Ingrese su contraseña por favor");
        binding.pwd2.setHint("Reconfirma la contraseña");

        return view;
    }

    @Override
    protected void next() {
        Report.click("ACTION_GG_012");
        String p0 = binding.pwd.getText().toString().trim();
        String p1 = binding.pwd2.getText().toString().trim();
        boolean valid = true;
        binding.err0.setVisibility(View.GONE);
        binding.err1.setVisibility(View.GONE);
        boolean bo1 = isEmpty(binding.pwd, binding.err0);
        boolean bo2 = isEmpty(binding.pwd2, binding.err1);
        if (!bo1 && !bo2) {
            bo1 = isValid(binding.pwd, binding.err0);
            bo2 = isValid(binding.pwd2, binding.err1);
            if (bo1 && bo2) {
                if (!TextUtils.equals(p0, p1)) {
                    valid = false;
                    binding.err0.setVisibility(View.GONE);
                    binding.err1.setVisibility(View.VISIBLE);
                    binding.err1.setText("Las contraseñas que ingresó no coinciden. Verifique y vuelva a ingresarlas");
                }
            } else {
                valid = false;
            }
        } else {
            valid = false;
        }
        if (!valid) {
            return;
        }
        showLoading();
        new RegReq(phone, p0, code).req(new ReqCallback<RegBean>() {
            @Override
            public void callback(boolean success, RegBean bean) {
                if (success && bean.isValid()) {
                    Report.show("ACTION_GG_013");
                    Report.adjustEvent("6ar8p4");
                    bean.usr.phone = phone;
                    if (bean.usr.isAdmin) {
                        new GetWhiteReq().req(new ReqCallback<WhiteDataBean>() {
                            @Override
                            public void callback(boolean success, WhiteDataBean data) {
                                hideLoading();
                                if (success) {
                                    SPHelper.getInstance(App.getApp()).putStringAndCommit("usr", new Gson().toJson(bean.usr));
                                    Constant.white = true;
                                    ((LoginActivity) getActivity()).toHome();
                                } else {
                                    ToastUtil.showShortToast(getActivity(), "Solicitud incorrecta");
                                }
                            }
                        });
                    } else {
                        hideLoading();
                        SPHelper.getInstance(App.getApp()).putStringAndCommit("usr", new Gson().toJson(bean.usr));
                        ((LoginActivity) getActivity()).toHome();
                    }
                } else {
                    hideLoading();
                }
            }
        });
    }
}
